

package com.hazelcast.jet.impl.submitjob.clientside;

import com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl;
import com.hazelcast.cluster.Member;
import com.hazelcast.jet.JetException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.UUID;

/**
 * Used by client side to find a target cluster member
 */
public class SubmitJobTargetMemberFinder {

    /**
     * Pick a random cluster member
     */
    public UUID getRandomMemberId(HazelcastClientInstanceImpl client) {
        ArrayList<Member> memberList = new ArrayList<>(client.getClientClusterService().getMemberList());
        return getRandomIdFromList(memberList);
    }

    UUID getRandomIdFromList(ArrayList<Member> memberList) {
        if (memberList.isEmpty()) {
            throw new JetException("Cluster member list is empty");
        }
        Collections.shuffle(memberList);
        Member member = memberList.get(0);
        return member.getUuid();
    }
}
